package com.fitapp;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class StatsActivity extends Activity
{

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
	}

	@Override
	protected void onResume()
	{
		super.onResume( );
		setContentView( R.layout.activity_stats );

		DatabaseHelper helper = new DatabaseHelper( this );
		SQLiteDatabase db = helper.getWritableDatabase( );
		Cursor cursor = db.rawQuery( "SELECT * FROM Goals", null );
		if( cursor.getCount( ) > 0 )
		{
			cursor.moveToFirst( );
			do
			{
				if( cursor.getString( 1 ).equals( DatabaseHelper.TYPE_BENDING ) )
				{
					EditText bending;
					bending = (EditText) findViewById( R.id.editBending );
					bending.setText( cursor.getString( 4 ) );
					;
				}
				else if( cursor.getString( 1 ).equals( DatabaseHelper.TYPE_PRESSUP ) )
				{
					TextView bending;
					bending = (TextView) findViewById( R.id.editPressing );
					bending.setText( cursor.getString( 4 ) );
					;
				}
				else if( cursor.getString( 1 ).equals( DatabaseHelper.TYPE_RUNNING ) )
				{
					TextView bending;
					bending = (TextView) findViewById( R.id.editRunning );
					bending.setText( cursor.getString( 4 ) );
					;
				}
				else if( cursor.getString( 1 ).equals( DatabaseHelper.TYPE_SITUP ) )
				{
					// TODO
					TextView bending;
					bending = (TextView) findViewById( R.id.editSitup );
					bending.setText( cursor.getString( 4 ) );
					;
				}
			}
			while( cursor.moveToNext( ) );
			cursor.close( );
			db.close( );
		}

	}

	public void onBtnSaveClicked( View view )
	{
		boolean dataWellFormed = true;
		DatabaseHelper helper = new DatabaseHelper( this );
		SQLiteDatabase db = helper.getWritableDatabase( );
		ContentValues values = new ContentValues( );
		// TODO dirty!
		TextView bending;
		bending = (TextView) findViewById( R.id.editRunning );
		values.put( "Duration", 30 );
		String val = bending.getText( ).toString( );
		if( val.isEmpty( ) || Double.parseDouble( val ) > 9999 )
		{
			dataWellFormed = false;
		}
		else
		{
			values.put( "Intended", Integer.parseInt( bending.getText( ).toString( ) ) );
			db.update( "Goals", values, "Type=?", new String[ ] { DatabaseHelper.TYPE_RUNNING } );
		}
		bending = (TextView) findViewById( R.id.editBending );
		values.put( "Duration", 30 );
		val = bending.getText( ).toString( );
		if( val.isEmpty( ) || Double.parseDouble( val ) > 9999 )
		{
			dataWellFormed = false;
		}
		else
		{
			values.put( "Intended", Integer.parseInt( bending.getText( ).toString( ) ) );
			db.update( "Goals", values, "Type=?", new String[ ] { DatabaseHelper.TYPE_BENDING } );
		}
		bending = (TextView) findViewById( R.id.editPressing );
		values.put( "Duration", 30 );
		val = bending.getText( ).toString( );
		if( val.isEmpty( ) || Double.parseDouble( val ) > 9999 )
		{
			dataWellFormed = false;
		}
		else
		{
			values.put( "Intended", Integer.parseInt( bending.getText( ).toString( ) ) );
			db.update( "Goals", values, "Type=?", new String[ ] { DatabaseHelper.TYPE_PRESSUP } );
		}
		bending = (TextView) findViewById( R.id.editSitup );
		values.put( "Duration", 30 );
		val = bending.getText( ).toString( );
		if( val.isEmpty( ) || Double.parseDouble( val ) > 9999 )
		{
			dataWellFormed = false;
		}
		else
		{
			values.put( "Intended", Integer.parseInt( bending.getText( ).toString( ) ) );
			db.update( "Goals", values, "Type=?", new String[ ] { DatabaseHelper.TYPE_SITUP } );
		}
		if( dataWellFormed )
		{
			Intent i = new Intent( StatsActivity.this, MainActivity.class );
			startActivity( i );
			StatsActivity.this.onDestroy( );
		}
		else
		{
			Toast toast = Toast.makeText( StatsActivity.this, "Please insert a Value lower then 10000", Toast.LENGTH_SHORT );
			toast.show( );
		}
		db.close( );
		helper.close( );
	}
}
